#include "bsp_gpio.h"
#include <stdio.h>
#include "stdarg.h" //自定义printf需要使用
/******************************************************************************
 *	函数功能 相当于串口发送数据
 *	参数
 *	返回值
 *	注意 该函数是stdio.h文件中默认函数 当我们使用printf函数时会自调用
 *		 这里的printf函数跟C语言的printf用法一样
 ******************************************************************************/

#if 1
#pragma import(__use_no_semihosting)
// 标准库需要的支持函数
struct __FILE
{
	int handle;
	/* Whatever you require here. If the only file you are using is */
	/* standard output using printf() for debugging, no file handling */
	/* is required. */
};
/* FILE is typedef’ d in stdio.h. */
FILE __stdout;
// 定义_sys_exit()以避免使用半主机模式
void _sys_exit(int x)
{
	x = x;
}

int fputc(int ch, FILE *f)
{
	while ((USART3->SR & 0X40) == 0)
		; // 循环发送,直到发送完毕
	USART3->DR = (u8)ch;
	return ch;
}
#endif

void bsp_usart_gpio_init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;

	RCC_AHB1PeriphClockCmd(DEBUG_UART_GPIO_CLK, ENABLE);

	GPIO_PinAFConfig(DEBUG_UART_TX_PORT, GPIO_PinSource10, GPIO_AF_USART3);
	GPIO_PinAFConfig(DEBUG_UART_RX_PORT, GPIO_PinSource11, GPIO_AF_USART3);
	
	// USART1端口配置
	GPIO_InitStructure.GPIO_Pin = DEBUG_UART_TX_PIN | DEBUG_UART_RX_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;		// 复用功能
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;	// 速度50MHz
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;		// 推挽复用输出
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;		// 上拉
	GPIO_Init(DEBUG_UART_TX_PORT, &GPIO_InitStructure); // 初始化PA9，PA10


	RCC_AHB1PeriphClockCmd(UART_GPIO_CLK, ENABLE);

  	/* Connect USART pins to AF7 */
	GPIO_PinAFConfig(UART_TX_PORT, UART_TX_SOURCE, UART_TX_AF);
	GPIO_PinAFConfig(UART_RX_PORT, UART_RX_SOURCE, UART_RX_AF);

	// USART1端口配置
	GPIO_InitStructure.GPIO_Pin = UART_TX_PIN | UART_RX_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;		// 复用功能
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;	// 速度50MHz
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;		// 推挽复用输出
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;		// 上拉
	GPIO_Init(UART_TX_PORT, &GPIO_InitStructure); // 初始化PA9，PA10
}

void debug_usart_init(uint32_t baud)
{
	RCC_APB1PeriphClockCmd(DEBUG_UART_INIT_CLK, ENABLE);

	USART_InitTypeDef USART_InitStructure;

	/* Enable the USART OverSampling by 8 */
	USART_OverSampling8Cmd(USART3, ENABLE); 

	USART_InitStructure.USART_BaudRate = baud;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;

	USART_Init(USART3, &USART_InitStructure);

	USART_Cmd(USART3, ENABLE);
}

void USART_Config(uint32_t baud)
{
  RCC_AHB1PeriphClockCmd(UART_INIT_CLK, ENABLE);

  USART_InitTypeDef USART_InitStructure;
  
  /* Enable the USART OverSampling by 8 */
  USART_OverSampling8Cmd(USART2, ENABLE);  

  USART_InitStructure.USART_BaudRate = baud;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  USART_Init(USART2, &USART_InitStructure);
   
  /* Enable USART */
  USART_Cmd(USART2, ENABLE);
}

void bsp_uart_init(void)
{
	bsp_usart_gpio_init();
	debug_usart_init(115200);
	USART_Config(115200);
	LOG("%s %s",__DATE__ ,__TIME__);
}
